using Microsoft.AspNetCore.Mvc;
using 高校员工餐饮信息管理系统.Models;
using 高校员工餐饮信息管理系统.Services;

namespace 高校员工餐饮信息管理系统.Controllers
{
    /// <summary>
    /// 认证控制器
    /// 演示依赖注入的使用
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AuthController : ControllerBase
    {
        private readonly IUserService _userService;
        private readonly IJwtService _jwtService;
        private readonly ILogger<AuthController> _logger;

        /// <summary>
        /// 构造函数注入依赖服务
        /// 这展示了依赖注入如何实现业务逻辑与控制器解耦
        /// </summary>
        public AuthController(
            IUserService userService, 
            IJwtService jwtService,
            ILogger<AuthController> logger)
        {
            _userService = userService;
            _jwtService = jwtService;
            _logger = logger;
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        [HttpPost("login")]
        public async Task<IActionResult> Login([FromBody] LoginRequest request)
        {
            _logger.LogInformation($"登录请求: {request.WorkId}");

            if (string.IsNullOrEmpty(request.WorkId) || string.IsNullOrEmpty(request.Password))
            {
                return BadRequest(new { Message = "工号和密码不能为空" });
            }

            // 通过依赖注入的服务进行用户认证
            var user = await _userService.AuthenticateAsync(request.WorkId, request.Password);
            
            if (user == null)
            {
                _logger.LogWarning($"登录失败: 工号或密码错误 - {request.WorkId}");
                return Unauthorized(new { Message = "工号或密码错误" });
            }

            // 生成JWT令牌
            var token = _jwtService.GenerateToken(user);

            _logger.LogInformation($"登录成功: {user.WorkId} ({user.Role})");

            return Ok(new
            {
                Message = "登录成功",
                Token = token,
                User = new
                {
                    user.WorkId,
                    user.UserName,
                    user.Email,
                    user.Role,
                    user.IsFirstLogin
                }
            });
        }

        /// <summary>
        /// 重置密码
        /// </summary>
        [HttpPost("reset-password")]
        public async Task<IActionResult> ResetPassword([FromBody] ResetPasswordRequest request)
        {
            _logger.LogInformation($"密码重置请求: {request.WorkId}");

            if (string.IsNullOrEmpty(request.WorkId) || string.IsNullOrEmpty(request.Email))
            {
                return BadRequest(new { Message = "工号和邮箱不能为空" });
            }

            var result = await _userService.ResetPasswordAsync(request.WorkId, request.Email);

            if (result)
            {
                _logger.LogInformation($"密码重置邮件已发送: {request.Email}");
                return Ok(new { Message = "密码重置邮件已发送，请查收邮箱" });
            }

            _logger.LogWarning($"密码重置失败: {request.WorkId}");
            return BadRequest(new { Message = "密码重置失败，请检查工号和邮箱是否正确" });
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        [HttpPost("change-password")]
        public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordRequest request)
        {
            _logger.LogInformation($"修改密码请求: {request.WorkId}");

            if (string.IsNullOrEmpty(request.WorkId) || 
                string.IsNullOrEmpty(request.OldPassword) || 
                string.IsNullOrEmpty(request.NewPassword))
            {
                return BadRequest(new { Message = "所有字段不能为空" });
            }

            var result = await _userService.ChangePasswordAsync(
                request.WorkId, 
                request.OldPassword, 
                request.NewPassword);

            if (result)
            {
                _logger.LogInformation($"密码修改成功: {request.WorkId}");
                return Ok(new { Message = "密码修改成功" });
            }

            _logger.LogWarning($"密码修改失败: {request.WorkId}");
            return BadRequest(new { Message = "密码修改失败，请检查旧密码是否正确" });
        }
    }

    /// <summary>
    /// 重置密码请求模型
    /// </summary>
    public class ResetPasswordRequest
    {
        public string WorkId { get; set; } = string.Empty;
        public string Email { get; set; } = string.Empty;
    }

    /// <summary>
    /// 修改密码请求模型
    /// </summary>
    public class ChangePasswordRequest
    {
        public string WorkId { get; set; } = string.Empty;
        public string OldPassword { get; set; } = string.Empty;
        public string NewPassword { get; set; } = string.Empty;
    }
}

